<template>
	<view class="container">
		<image class="logo" :src="static+'/logo.png'"></image>
		<view class="form_unit">
			<view class="form_label">+86</view>
			<input type="number" data-name="phone" :value="phone" @input="input_" placeholder="请输入登录手机号码"/>
		</view>
		<view class="form_unit">
			<view class="form_label">登录密码</view>
			<input type="text" password="true" data-name="pwd" :value="password" placeholder="请输入登录密码" @input="input_"/>
		</view>
		<view class="operate">
			<view class="register" v-on:click="forget">忘记密码</view>
			<view class="verify_login" v-on:click="verify_login">验证码登录</view>
		</view>
		<!--  #ifdef  APP-PLUS -->
		<view class="operate_login">
			<view class="login_button" v-on:click="login_submit">登录</view>
		</view>
		<view class="forget" v-on:click="register">没有账号？马上注册</view>
		<!-- #endif-->
		<!--  #ifdef H5 -->
		<view class="operate_login">
			<view class="login_button" v-on:click="login_submit">登录</view>
		</view>
		<view class="forget" v-on:click="register">没有账号？马上注册</view>
		<!-- #endif-->
		<!--  #ifdef  MP-WEIXIN -->
		<view class="operate_login">
			<view class="wechat_button" v-on:click="wechat_login">
				<image src="../../static/images/wechat.png"></image>
				微信登录
			</view>
		</view>
		<!-- #endif-->
		<!-- <view class="forget" v-on:click="register">没有账号？马上注册</view> -->
	</view>
</template>
<style>
	page{background: #F5F5F5;}
	.container{background: #F5F5F5;display: flex;flex-direction: column;justify-content: space-between;align-items: center;}
	.logo{width:200upx;height:200upx;margin-top:200upx;}
	.form_unit{width: 75%;margin:10upx 12.5%;border-bottom: 1px solid #e5e5e5 ;display: flex;justify-content: space-between;align-items: center;height: 80upx;}
	.form_label{width:25%;font-size: 28upx;color: #282828;text-align: center;border-right: 1px solid #e5e5e5 ;}
	input{font-size: 28upx;text-align: left;width: 70%;color: #282828;}
	.operate{width: 75%;margin:10upx 12.5%;height: 80upx;display: flex;justify-content: space-between;align-items: center;}
	.register,.verify_login{font-size: 28upx;color: #282828;text-decoration: underline;}
	
	.operate_login{width: 75%;margin:10upx 12.5%;height: 80upx;display: flex;justify-content: center;align-items: center;}
	.login_button{width:100%;height: 80upx;font-size: 30upx;line-height: 80upx;background-image: linear-gradient(#43eac9, #24c3a4);color: #FFFFFF;border-radius: 40upx;text-align: center;}
	.wechat_button{width:100%;height: 80upx;font-size: 30upx;border-radius: 40upx;display: flex;justify-content: center;align-items: center;
	background-image: linear-gradient(#43eac9, #24c3a4);color: #FFFFFF;}
	.wechat_button image{width:50upx;height: 50upx;margin-right: 10upx;}
	.forget{width: 75%;margin:10upx 12.5%;height: 80upx;line-height: 80upx;text-align: center;font-size: 28upx;text-decoration: underline;color: #282828;}
	.service{font-size: 24upx;width:100%;text-align: center;position: fixed;bottom: 20upx;color: #282828;}
</style>
<script>
	export default {
		data() {
			return {
				member_id:0,
				pic_h:"",
				radius:'',
				menu_h:'',
				phone:'',
				password:'',
				device_id:'',
				code:'',
				url: getApp().globalData.url,
				static:getApp().globalData.static
			}
		},
		onLoad() {
			var res = uni.getSystemInfoSync();
			this.pic_h   = res.windowWidth*0.76*0.2+"px";
			this.menu_h  = res.windowWidth*0.96*0.24+"px";
			this.radius  = res.windowWidth*0.86*0.2/2+"px";
			
			//#ifdef H5
			this.code = this.getUrlParam('code');
			if(this.code.length>10)
			{
				this.initialize();
			}
			//#endif
		},
		methods: {
			getUrlParam:function(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]); return null;
			},
			initialize:function()
			{
				var that = this;
				var member_id = uni.getStorageSync("member_id");
				uni.request({
					url: that.url+'/wechat_snsapi_userinfo_get_openid', 
					data: {code:this.code},
					header: {'Content-Type': 'application/json'},
					success: (res) => {
						if(res.data.status==1)
						{
							uni.setStorageSync("member_id",res.data.result.member_id);
							uni.setStorageSync("avatarUrl",res.data.result.img);
							uni.setStorageSync("nickname",res.data.result.nickname);
							uni.setStorageSync("grade",res.data.result.grade);
							uni.setStorageSync("integral",res.data.result.integral);
							uni.setStorageSync("phone",res.data.result.phone);
							uni.setStorageSync("balance",res.data.result.balance);
							uni.setStorageSync("upid",res.data.result.upid);
							uni.setStorageSync("openid",res.data.result.openid);
							uni.redirectTo({
								url:"index"
							})
						}
						else
						{
							uni.setStorageSync("openid",res.data.result.openid);
							uni.setStorageSync("nickname",res.data.result.nickname);
							uni.setStorageSync("avatarUrl",res.data.result.headimgurl);
							uni.navigateTo({
								url:"register"
							})
						}
						
					}
				});
			},
			wechat_login:function()
			{
				var upid = uni.getStorageSync("upid");
				var member_id = uni.getStorageSync("member_id");
				if(member_id=="")
				{
					member_id = 0;
				}
				if(upid=="undefined")
				{
					upid = 0;
				}
				var code = '';
				var that = this;
				var useinfo = {};
				// uni.requestSubscribeMessage({
				// 	tmplIds: ['InK-z2h-Nncf9MnmSCvM8PSZsEXDONjTX3cLz4HLseM',//订单支付提醒
				// 			  'mkP9bar4566G3uawjVlaiG4jwukCyQfTl1JPxIR-pKI',//签到提醒
				//               'wG0Vy3NOX8s0PmpyDn4BjPDE37oSe_A7yHUV5aiDeok']//优惠活动通知
				// })

				wx.getUserProfile({
					lang: 'zh_CN',
					desc:'获取用户展示信息',
					success: function (e) 
					{
						console.log(e);
						useinfo = e.userInfo;
						uni.login({
						    success: function (res) 
							{
								console.log(res);
								code = res.code;
								uni.request({
									url: that.url+"/wechat_get_openid",
									data: {
										provider:'weixin',
										member_id:member_id,
										code: code,
										nickname: useinfo.nickName,
										gender: useinfo.gender,
										avatarUrl: useinfo.avatarUrl,
										upid:upid
									},
									header: {
									  'Content-Type': 'application/json'
									},
									success: function (res) 
									{
										console.log(res.data.member_id);
										uni.setStorageSync("member_id",res.data.member_id);
										uni.setStorageSync("avatarUrl",res.data.img);
										uni.setStorageSync("nickname",res.data.nickname);
										uni.setStorageSync("grade",res.data.grade);
										uni.setStorageSync("integral",res.data.integral);
										uni.setStorageSync("phone",res.data.phone);
										uni.setStorageSync("upid",res.data.upid);
										that.avatarUrl = res.data.img;
										that.member_id = res.data.member_id;
										that.nickname  = res.data.nickname;
										that.grade       = res.data.grade;
										that.grade_name  = res.data.grade_name;
										that.integral    = res.data.integral;
										that.phone    = res.data.phone;
										that.display = 0;
										that.con_display = 0;
										uni.navigateBack();
									}
								})
						    }
						})
					},
					fail:function(e)
					{
						console.log(e);
					}
				});
			},
			verify_login:function()
			{
				uni.navigateTo({
					url:'verify_login'
				})
			},
			forget:function()
			{
				uni.navigateTo({
					url:'forget'
				})
			},
			register:function()
			{
				uni.navigateTo({
					url:'register'
				})
			},
			input_:function(e)
			{
				if(e.currentTarget.dataset.name=="phone")
				{
					this.phone = e.target.value;
				}
				if(e.currentTarget.dataset.name=="pwd")
				{
					this.password = e.target.value;
				}
			},
			login_submit:function()
			{
				var that = this;
				uni.request({
					url: this.url+'/wechat_member_login', 
					method:"POST",
					data: {phone:this.phone,password:this.password,device_id:this.device_id},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: (res) => {
						console.log(res.data);
						if(res.data.status==1)
						{
							that.st = 1;
							uni.showToast({
								title:"登录成功",
								duration:2000
							})
							uni.setStorageSync("member_id",res.data.result[0].member_id);
							uni.setStorageSync("grade",res.data.result[0].grade);
							uni.reLaunch({url:'/pages/member/index'});
						}
						else
						{
							uni.showModal({
								title:"登录提示",
								content:res.data.msg,
								showCancel:false})
						}
					}
				});
			}
		}
	}
</script>


